/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#pragma once


#include <map>
#include <string>

#include <kmhdfs/common/hash.h>
#include <kmhdfs/client/kerberos_name.h>
#include <kmhdfs/client/token.h>

namespace Hdfs {
    namespace Internal {
        class UserInfo {
        public:
            UserInfo() {
            }

            explicit UserInfo(const std::string &u) : effectiveUser(u) {
            }

            const std::string &getRealUser() const {
                return realUser;
            }

            void setRealUser(const std::string &user) {
                this->realUser = user;
            }

            const std::string &getEffectiveUser() const {
                return effectiveUser.getName();
            }

            void setEffectiveUser(const std::string &effectiveUser) {
                this->effectiveUser = KerberosName(effectiveUser);
            }

            std::string getPrincipal() const {
                return effectiveUser.getPrincipal();
            }

            bool operator ==(const UserInfo &other) const {
                return realUser == other.realUser
                       && effectiveUser == other.effectiveUser;
            }

            void addToken(const Token &token) {
                tokens[std::make_pair(token.getKind(), token.getService())] = token;
            }

            const Token *selectToken(const std::string &kind, const std::string &service) const {
                std::map<std::pair<std::string, std::string>, Token>::const_iterator it;
                it = tokens.find(std::make_pair(kind, service));

                if (it == tokens.end()) {
                    return NULL;
                }

                return &it->second;
            }

            size_t hash_value() const;

        public:
            static UserInfo LocalUser();

        private:
            KerberosName effectiveUser;
            std::map<std::pair<std::string, std::string>, Token> tokens;
            std::string realUser;
        };
    }
}

HDFS_HASH_DEFINE(::Hdfs::Internal::UserInfo);
